package xin.yangshuai.springsecurity01.config;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import xin.yangshuai.common01.entity.BaseResult;

import java.io.IOException;

public class MySessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {

        BaseResult<Object> result = new BaseResult<>();
        result.setCode("-1");
        result.setMessage("该账号已从其它设备登录");

        String json = JSON.toJSONString(result);

        HttpServletResponse response = event.getResponse();

        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
    }
}
